package au.com.handsunited.web.controller.group;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.Group;
import au.com.handsunited.service.EntityService;
import au.com.handsunited.service.GroupService;

/**
 * @author arunda
 *
 */
public class GroupListController extends AbstractController {

	private EntityService entityService;
	private GroupService groupService;
	private String successView;

	private static Logger logger = LoggerFactory.getLogger(GroupListController.class);


	/**
	 *
	 */
	public GroupListController() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		List<Group> groups = null;

		if (StringUtils.isNotEmpty(id)) {
			BaseEntity entity = entityService.getEntity(id);
			if (entity != null) {
				groups = entity.getGroupsInNetwork();
			}
		} else {
			groups = groupService.getAllGroups();
		}

		ModelAndView mav = new ModelAndView(successView, "groups", groups);

		return mav;
	}

	/**
	 * @param groupService the groupService to set
	 */
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	/**
	 * @param entityService the entityService to set
	 */
	public void setEntityService(EntityService entityService) {
		this.entityService = entityService;
	}

	/**
	 * @param successView the successView to set
	 */
	public void setSuccessView(String successView) {
		this.successView = successView;
	}

}
